package com.example.block_chain.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/*
 *@description: mq配置文件
 *@author: hisen
 *@Time: 2023/11/22  21:04
 */
@Configuration
public class MQConfig {
    //配置消息转换器
    @Bean
    public Jackson2JsonMessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, Jackson2JsonMessageConverter messageConverter) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(messageConverter);
        return rabbitTemplate;
    }
    //订阅队列
    @Bean("schoolAQueue")
    public Queue schoolQueue() {
        return new Queue("schoolAQueue");
    }

    //扇形交换机
    @Bean("schoolAExchange")
    public FanoutExchange schoolExchange() {
        return new FanoutExchange("schoolAExchange");
    }

    //绑定两个subscribeQueue队列到subscribeExchange交换机
    @Bean
    public Binding Binding(
            @Qualifier("schoolAQueue") Queue subscribeQueue,
            @Qualifier("schoolAExchange") FanoutExchange subscribeExchange) {
        return BindingBuilder.bind(subscribeQueue).to(subscribeExchange);
    }

}
